package com.gitee.wsl.net

import io.ktor.client.plugins.cache.storage.CachedResponseData
import io.ktor.http.HeadersBuilder
import io.ktor.http.HttpProtocolVersion
import io.ktor.http.HttpStatusCode
import io.ktor.http.Url
import io.ktor.util.date.GMTDate
import io.ktor.util.flattenEntries
//import okio.BufferedSink
//import okio.BufferedSource
//
//
//internal fun BufferedSource.readCache(): CachedResponseData {
//    val source = this
//    val url = source.readUtf8Line()!!
//    val status = HttpStatusCode(source.readInt(), source.readUtf8Line()!!)
//    val version = HttpProtocolVersion.parse(source.readUtf8Line()!!)
//    val headersCount = source.readInt()
//    val headers = HeadersBuilder()
//    for (j in 0 until headersCount) {
//        val key = source.readUtf8Line()!!
//        val value = source.readUtf8Line()!!
//        headers.append(key, value)
//    }
//    val requestTime = GMTDate(source.readLong())
//    val responseTime = GMTDate(source.readLong())
//    val expirationTime = GMTDate(source.readLong())
//    val varyKeysCount = source.readInt()
//    val varyKeys = buildMap {
//        for (j in 0 until varyKeysCount) {
//            val key = source.readUtf8Line()!!
//            val value = source.readUtf8Line()!!
//            put(key, value)
//        }
//    }
//    val bodyCount = source.readInt()
//    val body = ByteArray(bodyCount)
//    source.readFully(body)
//    return CachedResponseData(
//        url = Url(url),
//        statusCode = status,
//        requestTime = requestTime,
//        responseTime = responseTime,
//        version = version,
//        expires = expirationTime,
//        headers = headers.build(),
//        varyKeys = varyKeys,
//        body = body
//    )
//}
//
//internal fun BufferedSink.writeCache(cache: CachedResponseData) {
//    val channel = this
//    channel.writeUtf8(cache.url.toString() + "\n")
//    channel.writeInt(cache.statusCode.value)
//    channel.writeUtf8(cache.statusCode.description + "\n")
//    channel.writeUtf8(cache.version.toString() + "\n")
//    val headers = cache.headers.flattenEntries()
//    channel.writeInt(headers.size)
//    for ((key, value) in headers) {
//        channel.writeUtf8(key + "\n")
//        channel.writeUtf8(value + "\n")
//    }
//    channel.writeLong(cache.requestTime.timestamp)
//    channel.writeLong(cache.responseTime.timestamp)
//    channel.writeLong(cache.expires.timestamp)
//    channel.writeInt(cache.varyKeys.size)
//    for ((key, value) in cache.varyKeys) {
//        channel.writeUtf8(key + "\n")
//        channel.writeUtf8(value + "\n")
//    }
//    channel.writeInt(cache.body.size)
//    channel.write(cache.body)
//}